package com.jiangyao.job;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jiangyao.common.utils.DateUtil;
import com.jiangyao.model.entity.SysLog;
import com.jiangyao.service.SysLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 日志清理定时任务
 *
 * @author: yangyongchuan
 * @create: 2020年07月24日 00:56
 */
@Component
public class SysLogJob {
	private static final Logger LOGGER = LoggerFactory.getLogger(SysLogJob.class);

	@Autowired
	private SysLogService sysLogService;


	/**
	 * 每天 凌晨2点10分执行一次：current
	 * 清除多余的系统操作日志：只保留2周以内的日志
	 */
	@Scheduled(cron = "0 10 2 * * ?")
	public void cleanSysLog() {
		LOGGER.info("===========清除系统操作日志定时任务 start===========");
		long start = System.currentTimeMillis();
		// 14天以前的日期
		Date pre14DayDate = DateUtil.addTime(null, 3, -14);
		UpdateWrapper<SysLog> updateWrapper = new UpdateWrapper<>();
		updateWrapper.le("create_time", pre14DayDate);
		sysLogService.remove(updateWrapper);
		LOGGER.info("===========清除系统操作日志定时任务 end===========总共耗时：{} 毫秒", System.currentTimeMillis() - start);
	}


}
